package com.app.mymvp.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.widget.TextView;

import com.app.base.utils.L;
import com.app.base.utils.T;
import com.app.base.utils.TUtil;
import com.app.mymvp.R;
import com.app.mymvp.entity.StatusEntity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.autolayout.AutoLayoutActivity;

import java.lang.reflect.Type;

import butterknife.ButterKnife;

/**
 * Created by HJ on 2016/4/5.
 * 基类Activity
 */
public abstract class BaseActivity<P extends BasePresenter, M extends BaseModel> extends AutoLayoutActivity implements BaseFragment.OnFragmentInteractionListener {
    public P presenter;
    public M model;
    public Context context;
    protected BaseFragment selectedFragment;
    protected TextView tv_head_title;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getDisplayInfo();
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        context = this;
        presenter = TUtil.getT(this, 0);
        model = TUtil.getT(this, 1);

        if (this instanceof BaseView) presenter.setVM(this, model);
        initView();
    }

    /**
     * 获取设备信息
     */
    public void getDisplayInfo() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int dpi = dm.densityDpi;
        L.d("dpi=" + dpi + "height" + dm.heightPixels + "width" + dm.widthPixels);
    }

    @Override
    public void onBackPressed() {
        if (selectedFragment == null || !selectedFragment.onBackPressed()) {
            if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
                super.onBackPressed();
            } else {
                getSupportFragmentManager().popBackStack();
                super.onBackPressed();
            }
        }
    }

    @Override
    public void setSelectedFragment(BaseFragment selectedFragment) {
        this.selectedFragment = selectedFragment;
    }

    public abstract int getLayoutId();

    public abstract void initView();

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 为tabFragment提供修改标题的方法
     *
     * @param name
     */
    public void setHeadTitle(String name) {
        if (tv_head_title == null) {
            tv_head_title = (TextView) findViewById(R.id.tv_head_title);
        }
        tv_head_title.setText(name);
    }

    /**
     * @param response
     */
    public StatusEntity onStatus(String response) {
        Type type = new TypeToken<StatusEntity>() {
        }.getType();
        Gson gson = new Gson();
        StatusEntity statusEntity = gson.fromJson(response, type);
        T.show(this, statusEntity.getMessage());
        if (!"200".equals(statusEntity.getStatus())) {
            if("301".equals(statusEntity.getStatus())) {
            }
            T.show(this, statusEntity.getMessage());
        }
        return statusEntity;
    }

    /**
     * @param response
     * @param listener
     */
    public void onStatus(String response, OnStatusListener listener) {
        listener.onPostExecute(onStatus(response));
    }

    public interface OnStatusListener {
        void onPostExecute(StatusEntity statusEntity);
    }
}